<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index jsp</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/jquery/1.4/jquery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/json2.js"></script>
</head>
<body>

welcome index.jsp 

<br/><br/>

<a href="${pageContext.request.contextPath}/user/index">now go show page<a/><br/><br/>
<a href="${pageContext.request.contextPath}/user/api/list.json">now go show api json<a/><br/><br/>
<a href="${pageContext.request.contextPath}/user/api/list.xml">now go show api xml<a/><br/><br/>
<a href="${pageContext.request.contextPath}/extuser/list">now go show Ext page<a/><br/><br/>

<a id="writeXml" href="#">writeXml<a/><br/><br/>

<div id="writeXmlRes"></div><br/><br/>

<a id="writeJson" href="#">writeJson<a/><br/><br/>

<div id="writeJsonRes"></div>

<script type="text/javascript" >

MvcUtil = {};
MvcUtil.showSuccessResponse = function (text, element) {
	MvcUtil.showResponse("success", text, element);
};
MvcUtil.showErrorResponse = function showErrorResponse(text, element) {
	MvcUtil.showResponse("error", text, element);
};
MvcUtil.showResponse = function(type, text, element) {
	var responseElementId = element.attr("id") + "Response";
	var responseElement = $("#" + responseElementId);
	if (responseElement.length == 0) {
		responseElement = $('<span id="' + responseElementId + '" class="' + type + '" style="display:none">' + text + '</span>').insertAfter(element);
	} else {
		responseElement.replaceWith('<span id="' + responseElementId + '" class="' + type + '" style="display:none">' + text + '</span>');
		responseElement = $("#" + responseElementId);
	}
	responseElement.fadeIn("slow");
};
MvcUtil.xmlencode = function(xml) {
	//for IE 
	var text;
	if (window.ActiveXObject) {
	    text = xml.xml;
	 }
	// for Mozilla, Firefox, Opera, etc.
	else {
	   text = (new XMLSerializer()).serializeToString(xml);
	}			
    return text.replace(/\&/g,'&'+'amp;').replace(/</g,'&'+'lt;')
        .replace(/>/g,'&'+'gt;').replace(/\'/g,'&'+'apos;').replace(/\"/g,'&'+'quot;');
};


$("#writeXml").click(function() {
	var link = $(this);
	$.ajax({ url: "http://127.0.0.1:8080/projectsimple/user/api/list?format=xml",
		beforeSend: function(req) { 
		req.setRequestHeader("Accept", "");
		},
		success: function(xml) {
			MvcUtil.showSuccessResponse(MvcUtil.xmlencode(xml), $("#writeXmlRes"));
		},
		error: function(xhr) { 
			alert(xhr.responseText);
		}
	});
	return false;
});

$("#writeJson").click(function() {
	var link = $(this);
	$.ajax({ url: "http://127.0.0.1:8080/projectsimple/user/api/list?format=json",
		beforeSend: function(req) { 
			req.setRequestHeader("Accept", "");
		},
		success: function(json) {
			MvcUtil.showSuccessResponse(JSON.stringify(json),  $("#writeJsonRes"));
		},
		error: function(xhr) { 
			alert(xhr.responseText);
		}
	});
	return false;
});


</script>



</body>
</html>